<?php

include('login_validation.php');
include('functions.php');

$bgcolor = "#229CBA";

$errors = array();

if (isset($_GET['success']) && $_GET['success'] == "congressadded") {
	$success = "The congress was successfully created!";
}

if (isset($_POST['insert']) && $_POST['insert'] == 'insert') {


	// login key required
	$loginKeyReq = getValor('LOGIN_KEY_REQUIRED');

	// login key
	$keyPassword = getValor('LOGIN_KEY');
	$keyPasswordConfirm = getValor('CONFIRMATION_KEY');

	if (strlen($keyPassword) != 0 || strlen($keyPasswordConfirm) != 0 || $keyPasswordConfirm == 1) {
			if (strlen($keyPassword) == 0) {
				array_push($errors, "The key password cannot be empty");
			}
			
			if (strlen($keyPasswordConfirm) == 0) {
				array_push($errors, "The confirmation key cannot be empty");
			}
			
			if (strlen($keyPasswordConfirm) > 0 && strlen($keyPassword) > 0 && $keyPasswordConfirm != $keyPassword) {
				array_push($errors, "The confirmation key and the key password must be the same");
			}
	}


	$name = getValor('NAME');

	if (strlen($name) == 0) {
		array_push($errors, "The name cannot be empty");
	} else if (congressNameExists($name)) {
		array_push($errors, "The name of the congress already exists.");
	}
	
	// Login Header Image
	$fileLoginHeader = strtolower($_FILES["LOGIN_HEADER"]["name"]);
	$extLoginHeader = strtolower(substr($fileLoginHeader,-3,3));
	$imagenLoginHeader = (date("dmYHis") + 1 ) . "." . $extLoginHeader;
	$targetPathLoginHeader = "images/congress_login/" . $imagenLoginHeader;

	// Login Left Panel Image
	$fileLoginLeftPanel = strtolower($_FILES["LOGIN_LEFT_PANEL"]["name"]);
	$extLoginLeftPanel = strtolower(substr($fileLoginLeftPanel,-3,3));
	$imagenLoginLeftPanel = (date("dmYHis") + 2 ).".".$extLoginLeftPanel;
	$targetPathLeftPanel = "images/congress_login/" . $imagenLoginLeftPanel;

	// Login Footer Image
	$fileLoginFooter = strtolower($_FILES["LOGIN_FOOTER"]["name"]);
	$extLoginFooter = strtolower(substr($fileLoginFooter,-3,3));
	$imagenLoginFooter = (date("dmYHis") + 3 ).".".$extLoginFooter;
	$targetPathFooter = "images/congress_login/" . $imagenLoginFooter;

	// Main Header Image
	$fileMainHeader = strtolower($_FILES["MAIN_HEADER"]["name"]);
	$extMainHeader = strtolower(substr($fileMainHeader,-3,3));
	$imagenMainHeader = (date("dmYHis") + 4 ).".".$extMainHeader;
	$targetPathMainHeader = "images/congress_header/" . $imagenMainHeader;

	$bgColor = "#" . getValor('BACKGROUND_COLOR');
	$primaryColor = "#" . getValor('PRIMARY_COLOR');
	$secondaryColor = "#" . getValor('SECONDARY_COLOR');
	
	$menuButtonColor = "#" . getValor('MENU_BUTTON_COLOR');
	$menuButtonGradientColor = "#" . getValor('MENU_BUTTON_GRADIENT_COLOR');
	
	$rollOverButtonColor = "#" . getValor('ROLLOVER_BUTTON_COLOR');
	$rollOverButtonGradientColor = "#" . getValor('ROLLOVER_BUTTON_GRADIENT_COLOR');

	$forwardEmail = getValor('FORWARD_EMAIL');
	if (strlen($forwardEmail) == 0) {
		array_push($errors, "The forward email cannot be empty");
	}
	
	
	$twitterLink = getValor('TWITTER_LINK');
	$showTwitterLink = getValor('SHOW_TWITTER_LINK');
	
	if ($showTwitterLink == 1 && strlen($twitterLink) == 0) {
		array_push($errors, "The twitter link cannot be empty");
	}
	
	$facebookLink = getValor('FACEBOOK_LINK');
	$showFacebookLink = getValor('SHOW_FACEBOOK_LINK');

	if ($showFacebookLink == 1 && strlen($facebookLink) == 0) {
		array_push($errors, "The facebook link cannot be empty");
	}

	$websiteLink = getValor('WEBSITE_LINK');
	$showWebSiteLink = getValor('SHOW_WEBSITE_LINK');

	if ($showWebSiteLink == 1 && strlen($websiteLink) == 0) {
		array_push($errors, "The website link cannot be empty");
	}

	if (sizeof($errors) == 0) {

	include('SimpleImage.php');

	if (move_uploaded_file($_FILES['LOGIN_HEADER']['tmp_name'], $targetPathLoginHeader)) {
		$image = new SimpleImage();
		$image->load($targetPathLoginHeader);
		$ancho = 708;
		$alto = 171;
		$image->resize($ancho, $alto);
		$image->save($targetPathLoginHeader);
	}
	
	if (move_uploaded_file($_FILES['LOGIN_LEFT_PANEL']['tmp_name'], $targetPathLeftPanel)) {
		$image = new SimpleImage();
		$image->load($targetPathLeftPanel);
		$ancho = 459;
		$alto = 174;
		$image->resize($ancho, $alto);
		$image->save($targetPathLeftPanel);
	}

	if (move_uploaded_file($_FILES['LOGIN_FOOTER']['tmp_name'], $targetPathFooter)) {
		$image = new SimpleImage();
		$image->load($targetPathFooter);
		$ancho = 708;
		$alto = 174;
		$image->resize($ancho, $alto);
		$image->save($targetPathFooter);
	}
	
	if (move_uploaded_file($_FILES['MAIN_HEADER']['tmp_name'], $targetPathMainHeader)) {
		$image = new SimpleImage();
		$image->load($targetPathMainHeader);
		$ancho = 880;
		$alto = 149;
		$image->resize($ancho, $alto);
		$image->save($targetPathMainHeader);
	}

	$insertSQL = sprintf("INSERT INTO CONGRESS (NAME, LOGIN_HEADER, LOGIN_LEFT_PANEL, LOGIN_FOOTER, MAIN_HEADER, BACKGROUND_IMAGE, BACKGROUND_COLOR, PRIMARY_COLOR, SECONDARY_COLOR, MENU_BUTTON_COLOR, MENU_BUTTON_GRADIENT_COLOR, ROLLOVER_BUTTON_COLOR, ROLLOVER_BUTTON_GRADIENT_COLOR, FORWARD_EMAIL, TWITTER_LINK, SHOW_TWITTER_LINK, FACEBOOK_LINK, SHOW_FACEBOOK_LINK, WEBSITE_LINK, SHOW_WEBSITE_LINK, WEBSITE_TAG, LOGIN_KEY, LOGIN_KEY_REQUIRED) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($name, "text"),
                       GetSQLValueString($imagenLoginHeader, "text"),
                       GetSQLValueString($imagenLoginLeftPanel, "text"),
                       GetSQLValueString($imagenLoginFooter, "text"),
                       GetSQLValueString($imagenMainHeader, "text"),
                       GetSQLValueString("", "text"),
                       GetSQLValueString($bgColor, "text"),
                       GetSQLValueString($primaryColor, "text"),
                       GetSQLValueString($secondaryColor, "text"),
                       GetSQLValueString($menuButtonColor, "text"),
                       GetSQLValueString($menuButtonGradientColor, "text"),
                       GetSQLValueString($rollOverButtonColor, "text"),
                       GetSQLValueString($rollOverButtonGradientColor, "text"),
                       GetSQLValueString($forwardEmail, "text"),
                       GetSQLValueString("http://" . $twitterLink, "text"),
                       GetSQLValueString($showTwitterLink, "int"),
                       GetSQLValueString("http://" . $facebookLink, "text"),
                       GetSQLValueString($showFacebookLink, "int"),
                       GetSQLValueString("http://" . $websiteLink, "text"),
                       GetSQLValueString($showWebSiteLink, "int"),
                       GetSQLValueString("", "text"),
                       GetSQLValueString($keyPassword, "text"),
                       GetSQLValueString($loginKeyReq, "int"));

		executeUpdateQuery($insertSQL);

		header("Location: congress_add.php?success=congressadded");

	}

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

	<title>CONGRESS ADMIN - ADD CONGRESS</title>

	<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
	<link href="accordion/style.css" rel="stylesheet" type="text/css" media="screen" />

	<script src="accordion/accordian.pack.js" language="javascript" type="text/javascript">
	</script>

	<style>
		body {
			padding: 0px;
			margin: 0px;
		}
		
		.bodybg {
			background: <?php echo $bgcolor; ?>
			filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='<?php echo $bgcolor; ?>', endColorstr='#FFFFFF');
			background: -webkit-gradient(linear, left top, left bottom, from(<?php echo $bgcolor; ?>), to(#FFFFFF));
			background: -moz-linear-gradient(top,  <?php echo $bgcolor ?>,  #FFFFFF);
			width:100%;
			height: 600px;
		}
	</style>

	<script type="text/javascript" src="jscolor/jscolor.js"></script>

</head>

<body>

	<div align="center" class="bodybg">

		<?php include('admin_header.php'); ?>

		<?php include('admin_menu.php'); ?>
		
		<?php include('congress_add_header.php'); ?>


		
	  <div id="basic-accordian" style="background-color: white;">
		
			<?php if (sizeof($errors) > 0) { include('errors.php'); } ?>
			
		<?php if (isset($success)) { include('success.php'); } ?>
			
            <form method="post" name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
			
              <table align="center" cellpadding="5">

                <tr valign="baseline">
                  <td nowrap align="left">LOGIN KEY REQUIRED:</td>
                  <td>
					<?php $isKeyReq = getValor('LOGIN_KEY_REQUIRED'); ?>
				  	<input type="radio" value="1" name="LOGIN_KEY_REQUIRED" <?php if ($isKeyReq == '' || $isKeyReq == '1') { echo "checked='checked'"; } ?> 
					/> Yes
					<input type="radio" value="0" name="LOGIN_KEY_REQUIRED" <?php if ($isKeyReq == '0') { echo "checked='checked'"; } ?> 
					/> No
				  </td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">KEY Password</td>
                  <td><input type="text" name="LOGIN_KEY" value="<?php echo getValor('LOGIN_KEY'); ?>" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">CONFIRMATION KEY</td>
                  <td><input type="text" name="CONFIRMATION_KEY" value="<?php echo getValor('CONFIRMATION_KEY'); ?>" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">NAME</td>
                  <td><input type="text" name="NAME" value="<?php echo getValor('NAME') ?>" size="32" maxlength="50"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left" title="The image to be placed at the top of the login page of the congress">LOGIN HEADER IMAGE (710px x 170px)</td>
                  <td><input type="file" name="LOGIN_HEADER" value="" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left" title="The image to be placed on the left side of the login page of the congress">LOGIN LEFT IMAGE (460px &times; 175px) </td>
                  <td><input type="file" name="LOGIN_LEFT_PANEL" value="" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left" title="The image to be placed at the bottom of the login page of the congress">LOGIN FOOTER IMAGE (710px &times; 175px) </td>
                  <td><input type="file" name="LOGIN_FOOTER" value="" size="32"></td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left" title="The image to be placed at the header of the congress once the user logs in">MAIN HEADER</td>
                  <td><input type="file" name="MAIN_HEADER" value="" size="32"></td>
                </tr>
                
				<?php if (1==2) { ?>
				<tr valign="baseline">
                  <td nowrap align="left">BACKGROUND_IMAGE (WTF)</td>
                  <td><input type="text" name="BACKGROUND_IMAGE" value="" size="32"></td>
                </tr>
				<?php } ?>
				
                <tr valign="baseline">
                  <td nowrap align="left" title="The background color of the congress main page with gradient becoming white">BACKGROUND COLOR</td>
                  <td><input type="text" name="BACKGROUND_COLOR" class="color" value="<?php echo getValor('BACKGROUND_COLOR'); ?>" size="32"></td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left" title="The background color of the menu">PRIMARY COLOR</td>
                  <td><input type="text" name="PRIMARY_COLOR" class="color" value="<?php echo getValor('PRIMARY_COLOR'); ?>" size="32"></td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left" title="The background color of the submenu">SECONDARY COLOR</td>
                  <td><input type="text" name="SECONDARY_COLOR" class="color" value="<?php echo getValor('SECONDARY_COLOR'); ?>" size="32"></td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left" title="The main color of the active button">ACTIVE MENU BUTTON COLOR</td>
                  <td><input type="text" name="MENU_BUTTON_COLOR" class="color" value="<?php echo getValor('MENU_BUTTON_COLOR'); ?>" size="32"></td>
                </tr>
				
                <tr valign="baseline">
                  <td nowrap align="left" title="The gradient color of the active button">ACTIVE MENU BUTTON GRADIENT COLOR</td>
                  <td><input type="text" name="MENU_BUTTON_GRADIENT_COLOR" class="color" value="<?php echo getValor('MENU_BUTTON_GRADIENT_COLOR'); ?>" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="left">ROLLOVER_BUTTON_COLOR</td>
                  <td><input type="text" name="ROLLOVER_BUTTON_COLOR" class="color" value="<?php echo getValor('ROLLOVER_BUTTON_COLOR'); ?>" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="left">ROLLOVER_BUTTON_GRADIENT_COLOR</td>
                  <td>
				  	<input type="text" name="ROLLOVER_BUTTON_GRADIENT_COLOR" class="color" value="<?php echo getValor('ROLLOVER_BUTTON_GRADIENT_COLOR'); ?>" 
				  		size="32">
				  </td>
                </tr>
                
				<tr valign="baseline">
                  <td nowrap align="left">FORWARD EMAIL</td>
                  <td>
				  	<input type="text" name="FORWARD_EMAIL" value="<?php echo getValor('FORWARD_EMAIL'); ?>"  size="32" maxlength="100"> (i.e. Forgot Password)
				  </td>
                </tr>
				
                <tr valign="baseline">
                  <td nowrap align="left">TWITTER LINK</td>
                  <td>
				  	<?php $isTwitterShow = getValor('SHOW_TWITTER_LINK'); ?>
					http:// <input type="text" name="TWITTER_LINK" value="<?php echo getValor('TWITTER_LINK'); ?>" size="32">
					<input type="radio" name="SHOW_TWITTER_LINK" value="1" <?php if ($isTwitterShow == '' || $isTwitterShow == '1') { echo "checked='checked'"; } ?>  /> Yes
					<input type="radio" name="SHOW_TWITTER_LINK" value="0" <?php if ($isTwitterShow == '0') { echo "checked='checked'"; } ?>/> No
				  </td>
                </tr>

                <tr valign="baseline">
                  <td nowrap align="left">FACEBOOK LINK</td>
				  <?php $isFacebookShow = getValor('SHOW_FACEBOOK_LINK'); ?>
                  <td>http:// <input type="text" name="FACEBOOK_LINK" value="<?php echo getValor('FACEBOOK_LINK'); ?>" size="32">
					<input type="radio" name="SHOW_FACEBOOK_LINK" value="1" <?php if ($isFacebookShow == '' || $isFacebookShow == '1') { echo "checked='checked'"; } ?> /> Yes
					<input type="radio" name="SHOW_FACEBOOK_LINK" value="0"  <?php if ($isFacebookShow == '0') { echo "checked='checked'"; } ?> /> No
				  </td>
                </tr>

				<tr valign="baseline">
                  <td nowrap align="left">WEBSITE LINK</td>
				  <?php $isWebsiteShow = getValor('SHOW_WEBSITE_LINK'); ?>
                  <td>http:// <input type="text" name="WEBSITE_LINK" value="<?php echo getValor('WEBSITE_LINK'); ?>" size="32">
					<input type="radio" name="SHOW_WEBSITE_LINK" value="1" <?php if ($isWebsiteShow  == '' || $isWebsiteShow  == '1') { echo "checked='checked'"; } ?> /> Yes
					<input type="radio" name="SHOW_WEBSITE_LINK" value="0"  <?php if ($isWebsiteShow  == '0') { echo "checked='checked'"; } ?> /> No<br />
				  </td>
				  
                </tr>


				<?php if (1==2) { ?>
                <tr valign="baseline">
                  <td nowrap align="left">WEBSITE TAG (WTF) </td>
                  <td><input type="text" name="WEBSITE_TAG" value="<?php echo getValor('WEBSITE_TAG'); ?>" size="32"></td>
                </tr>
				<?php } ?>

                <tr valign="baseline">
                  <td nowrap align="left">&nbsp;</td>
                  <td><input type="submit" value="Save"></td>
                </tr>
				
				<?php if (1==2) { ?>
				<tr>
					<td style="color: red;">WTF: What's this for?</td>
				</tr>
				<?php } ?>
				
				
              </table>
              <input type="hidden" name="insert" value="insert">
            </form>
            <p>&nbsp;</p>
	  </div>
		
	</div>

</body>
</html>
